package com.nc.cloud.admin.biz;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.nc.cloud.admin.entity.User;
import com.nc.cloud.admin.mapper.MenuMapper;
import com.nc.cloud.admin.mapper.UserMapper;
import com.nc.cloud.authority.client.jwt.UserAuthUtil;
import com.nc.cloud.common.biz.BaseBiz;
import com.nc.cloud.common.constant.UserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 *
 *
 * @author XYP
 * @email 23484376@qq.com
 * @date 2020-03-20 09:33:52
 */
@Service
public class UserBiz extends BaseBiz<UserMapper,User> {


    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserAuthUtil userAuthUtil;

    @Override
    public void insertSelective(User entity) {
        String password = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT).encode(entity.getPwd());
        entity.setPwd(password);
        super.insertSelective(entity);
    }

    @Override
    @CacheClear(pre="user{1.account}")
    public void updateSelectiveById(User entity) {
        super.updateSelectiveById(entity);
    }

    /**
     * 根据用户名获取用户信息
     * @param account
     * @return
     */
    @Cache(key="user{1}")
    public User getUserByUserName(String account){
        User user = new User();
        user.setAccount(account);
        return mapper.selectOne(user);
    }
}